package org.dromara.hm.controller;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.hm.domain.HmSaleOrderPickUp;
import org.dromara.hm.domain.vo.*;
import org.dromara.hm.domain.vo.saleOrder.HmSaleOrderPickUpVo;
import org.dromara.hm.domain.vo.saleOrder.HmSaleOrderVo;
import org.dromara.hm.service.IStatisticsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/statistics")
public class StatisticsController extends BaseController {

    private final IStatisticsService statisticsService;

    /**
     * 统计客户数量及分类数量
     */
    @GetMapping("/queryCustomerActivity")
    public R<Map<String, Long>> queryCustomerActivity()
    {
        return R.ok(statisticsService.queryCustomerActivity());
    }
    /**
     * 各类主产品当月库存及销量
     * 持可以切换商品筛选数据
     */
    @GetMapping("/queryProductActivity")
    public R<Map<String, Object>> queryProductActivity(@RequestParam(required = false) Long ProductId)
    {
        return R.ok(statisticsService.queryProductActivity(ProductId));
    }

    /**
     *
     *各产品库存详情
     */
    @GetMapping("/queryStockDetail")
    public TableDataInfo<HmStockVo> queryStockDetail(@RequestParam(required = false) Long ProductId)
    {
        return statisticsService.queryStockDetail(ProductId);
    }
    /**
     *
     *产品当月提货数量详情
     */
    @GetMapping("/queryPickDetail")
    public TableDataInfo<HmSaleOrderPickUp> queryPickDetail(@RequestParam(required = false) Long ProductId)
    {
        return statisticsService.queryPickDetail(ProductId);
    }


    /**
     * 根据年月查找销售，回款金额,销售数量或金额，根据产品id分组
     *type   num：销售数量  money：销售金额
     */
    @GetMapping("/querySaleNumOrMoney")
    public R<Map<String, Object>> querySaleNumOrMoney(@RequestParam(required = true) String type,@RequestParam(required = false) String date)
    {
        if(date == null){date= LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));}
        return R.ok(statisticsService.querySaleNumOrMoney(type,date));
    }


    /**
     *
     *店铺销量排名
     */
    @GetMapping("/queryDeptList")
    public TableDataInfo<HmSaleOrderVo> queryDeptList(@RequestParam(required = false) Integer size,@RequestParam(required = false) String date)
    {
        if(date == null){date= LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));}
        return statisticsService.queryDeptList(size,date);
    }

    /**
     *
     *店铺销量详情
     */
    @GetMapping("/queryList1")
    public TableDataInfo<HmSaleOrderVo> queryList1(@RequestParam(required = true) Long id, @RequestParam(required = false) String date)
    {
        if(date == null){
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.queryList1(id, date);
    }
    /**
     *
     *店铺销量  回款详情
     */

    @GetMapping("/queryList2")
    public TableDataInfo<HmSaleOrderPickUpVo> queryList2(@RequestParam(required = true) Long id, @RequestParam(required = false) String date) {
        if (date == null) {
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.queryList2(id, date);
    }
    /**
     *
     *店铺产品排名
     */
    @GetMapping("/selectProductSort")
    public TableDataInfo<HmSaleOrderVo> selectProductSort(@RequestParam(required = false) Integer size, @RequestParam(required = false) String date, @RequestParam(required = false) String productId) {
        if (date == null) {
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.selectProductSort(size, date,productId);
    }

    /**
     *
     *主产品单品销售额详情
     */
    @GetMapping("/queryList3")
    public TableDataInfo<HmSaleOrderVo> queryList3(@RequestParam(required = true) Long id, @RequestParam(required = true) Long productId, @RequestParam(required = false) String date) {
        if (date == null) {
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.queryList3(id, productId, date);
    }

    /**
     *
     *个人销量排名
     */
    @GetMapping("/selectPersonSort")
    public TableDataInfo<HmSaleOrderVo> selectPersonSort(@RequestParam(required = false) Integer size, @RequestParam(required = false) String date) {
        if (date == null) {
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.selectPersonSort(size, date);
    }
    /**
     *
     *个人销量详情
     */
    @GetMapping("/queryList4")
    public TableDataInfo<HmSaleOrderVo> queryList4(@RequestParam(required = true) Long id, @RequestParam(required = false) String date) {
        if (date == null) {
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.queryList4(id, date);
    }

    /**
     *
     *个人回款详情
     */
    @GetMapping("/queryList5")
    public TableDataInfo<HmSaleOrderPickUpVo> queryList5(@RequestParam(required = true) Long id, @RequestParam(required = false) String date) {
        if (date == null) {
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.queryList5(id, date);
    }

    /**
     *
     *个人服务人员数量排名
     */
    @GetMapping("/selectPersonServerSort")
    public TableDataInfo<HmSaleOrderVo> selectPersonServerSort(@RequestParam(required = false) Integer size, @RequestParam(required = false) String date) {
        if (date == null) {
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.selectPersonServerSort(size, date);
    }


    /**
     *
     *个人服务总客户详情
     */
    @GetMapping("/queryList6")
    public TableDataInfo<HmCustomerVo> queryList6(@RequestParam(required = true) Long id, @RequestParam(required = false) String date) {
        if (date == null) {
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.queryList6(id, date);
    }

    /**
     *
     *当月服务次数详情
     */
    @GetMapping("/queryList7")
    public TableDataInfo<HmTodoItemsVo> queryList7(@RequestParam(required = true) Long id, @RequestParam(required = false) String date) {
        if (date == null) {
            date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        }
        return statisticsService.queryList7(id, date);
    }

    /**
     * 已修改：统一的详情数据获取接口，增加分页参数
     */
    @GetMapping("/getHomeDataDetail")
    public R<Map<String, Object>> getHomeDataDetail(
        PageQuery pageQuery, // 增加分页查询参数
        @RequestParam(required = true) String type,
        @RequestParam(required = true) Long id,
        @RequestParam(required = true) String date,
        @RequestParam(required = false) Long productId
    ) {
        // 将分页参数传递给服务层
        return R.ok(statisticsService.getHomeDataDetail(pageQuery, type, id, date, productId));
    }

    /**
     * 查询退款退货统计和明细数据
     * @param storeId 店铺ID (null或'all'表示全部)
     * @param date 年份或年月 (YYYY 或 YYYY-MM)
     * @param dateMode year或month
     * @param pageQuery 分页参数
     */
    @GetMapping("/queryRefundAndReturnStats")
    public R<RefundAndReturnStatsResultVo> queryRefundAndReturnStats(
        @RequestParam(required = false) String storeId,
        @RequestParam(required = false) String salespersonId,
        @RequestParam(required = true) String date,
        @RequestParam(required = true) String dateMode,
        PageQuery pageQuery
    ) {
        return R.ok(statisticsService.queryRefundAndReturnStats(storeId, date, dateMode, pageQuery,salespersonId));
    }

}
